package com.xunhang.controller;

import com.xunhang.dto.Result;
import com.xunhang.entity.News;
import com.xunhang.service.impl.NewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/news")
@Tag(name = "新闻模块")
@CrossOrigin(origins = "*")
public class NewsController {
    @Autowired
    NewsService newsService;
    @Operation(summary = "根据id查询新闻接口",description = "根据id查询新闻")
    @GetMapping("/{id}")
    public Result queryNewsById(@PathVariable("id") Integer id){
        return newsService.queryNewsById(id);
    }

    @Operation(summary = "获取热门新闻接口",description = "获取热门新闻")
    @GetMapping("/getHot")
    public Result queryHotNews(){
        return newsService.queryHotNews();
    }

    @Operation(summary = "保存新闻接口",description = "保存或上传新闻")
    @PostMapping()
    public Result saveNews(@RequestBody News news){
        return newsService.saveNews(news);
    }

    @Operation(summary = "更新新闻",description = "更新新闻")
    @PutMapping
    public Result updateNews(@RequestBody News news){
        return newsService.updateNews(news);
    }

    @Operation(summary = "删除新闻",description = "删除新闻")
    @DeleteMapping("/{id}")
    public Result deleteNews(@PathVariable("id") Integer id){
        return newsService.deleteNews(id);
    }

    @Operation(summary = "根据类型查找新闻",description = "根据类型查找新闻，需要提供当前页码,根据时间进行查询，没提供时间则默认根据提供时间查询,现在total统一返回总页数")
    @GetMapping("/type")
    public Result queryNewsByType(@RequestParam(value = "tid",required = true) Integer tid,
                                  @RequestParam(value = "current", defaultValue = "1") Integer current,
                                  @RequestParam(value="previousTime",required = false) LocalDateTime previousTime,
                                  @RequestParam(value="followingTime",required = false) LocalDateTime followingTime

    ){
        return newsService.queryNewsByType(tid,current,previousTime,followingTime,1);
    }

    @Operation(summary = "新闻搜索",description = "根据关键词模糊搜索新闻，需要提供当前页码,现在total统一返回总页数")
    @GetMapping("/search")
    public Result SearchNews(@RequestParam(value = "key",required = false) String key,
    @RequestParam(value = "current", defaultValue = "1") Integer current)
    {
        return newsService.searchNews(key,current,1);
    }

    @Operation(summary = "根据浏览量和类型获取新闻",description = "根据浏览量和类型获取新闻,现在total统一返回总页数")
    @GetMapping("/type/pagesViews")
    public Result queryNewsByPageViewsAndType(@RequestParam(value = "tid",required = true) Integer tid,
                                       @RequestParam(value = "current", defaultValue = "1") Integer current){
        return newsService.queryNewsByType(tid,current,null,null,2);
    }

    @Operation(summary = "根据浏览量获取新闻",description = "根据浏览量获取新闻,现在total统一返回总页数")
    @GetMapping("/pagesViews")
    public Result queryNewsByPageViews(@RequestParam(value = "current", defaultValue = "1") Integer current){
        return newsService.queryAllNews(current,2);
    }


}
